package admin_sales;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Properties;

import javax.mail.*;
import javax.mail.internet.*;

import java.io.IOException;

import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;

import java.util.concurrent.TimeUnit;

import org.testng.*;
import org.testng.annotations.*;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;

import admin_sales.Orders_send_email.GetMulti;

public class Invoices_send_email extends ru.st.selenium.pages.TestBase {
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

    @Test
  public void testUntitled2() throws Exception {
	Actions actions = new Actions(driver);
    driver.manage().window().maximize();
    driver.get(baseUrl + "admin");
    driver.findElement(By.id("username")).clear();
    driver.findElement(By.id("username")).sendKeys("admin");
    driver.findElement(By.id("login")).clear();
    driver.findElement(By.id("login")).sendKeys("admin123");
    driver.findElement(By.cssSelector("input.form-button")).click();
    driver.findElement(By.cssSelector("a[title=\"close\"] > span")).click();
    TimeUnit.SECONDS.sleep(3);
    actions.moveToElement(driver.findElement(By.xpath("//ul[@id='nav']/li[2]/a/span"))).build().perform();
    driver.findElement(By.xpath("//ul[@id='nav']/li[2]/ul/li[2]/a/span")).click();
    assertEquals("Invoices", driver.findElement(By.cssSelector("h3.icon-head.head-sales-invoice")).getText());
    driver.findElement(By.id("sales_invoice_grid_filter_increment_id")).clear();
    driver.findElement(By.id("sales_invoice_grid_filter_increment_id")).sendKeys("100000003");
    driver.findElement(By.cssSelector("button[title=\"Search\"]")).click();
    TimeUnit.SECONDS.sleep(3);
    assertEquals("100000003", driver.findElement(By.xpath("//table[@id='sales_invoice_grid_table']/tbody/tr/td[2]")).getText());
    TimeUnit.SECONDS.sleep(3);
    assertEquals("View", driver.findElement(By.linkText("View")).getText());
    driver.findElement(By.linkText("View")).click();
    TimeUnit.SECONDS.sleep(3);
    driver.findElement(By.cssSelector("button[title=\"Send Email\"]")).click();
    assertTrue(closeAlertAndGetItsText().matches("^Are you sure you want to send Invoice email to customer[\\s\\S]$"));
    TimeUnit.SECONDS.sleep(5);
    class MailAuthenticator extends Authenticator {
 		 
	    public PasswordAuthentication getPasswordAuthentication() {
	         return new PasswordAuthentication("qatestingtestqa@gmail.com", "parol123");
	    }
	}
	
	Properties props = new Properties();
	
	props.put("mail.smtp.user", "qatestingtestqa@gmail.com");
	props.put("mail.host", "pop.gmail.com");
	props.put("mail.store.protocol", "pop3s");
    props.put("mail.pop3s.auth", "true");
    props.put("mail.pop3s.port", 995);
	props.put("mail.smtp.starttls.enable","true");
	props.put("mail.smtp.debug", "true");
	props.put("mail.smtp.auth", "true");
	props.put("mail.smtp.socketFactory.port", 995);
	props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
	props.put("mail.smtp.socketFactory.fallback", "false");

	Session session = Session.getInstance(props, new MailAuthenticator());
	session.setDebug(true);
       	  
	

	
        Store store = session.getStore("pop3s");
        store.connect("pop.gmail.com", "qatestingtestqa@gmail.com", "parol123");
 
        Folder inbox = store.getFolder("INBOX");
        inbox.open(Folder.READ_ONLY);
        
        Message[] messages = inbox.getMessages();
        
           
        GetMulti gmulti = new GetMulti();
        String textMessage = gmulti.getText(messages[messages.length - 1]);
        String regex = "Your Invoice #100000003 for Order #100000035";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(textMessage);
        if (m.find()) {
        	    driver.get(baseUrl);
        	    driver.findElement(By.id("search")).clear();
        	    driver.findElement(By.id("search")).sendKeys(m.group());
        	TimeUnit.SECONDS.sleep(5);
        	   
        }
        inbox.close(false);
        store.close(); 
        driver.get(baseUrl);
     //   driver.quit();  
}

private boolean isElementPresent(By by) {
try {
  driver.findElement(by);
  return true;
} catch (NoSuchElementException e) {
  return false;
}
}


private String closeAlertAndGetItsText() {
try {
  Alert alert = driver.switchTo().alert();
  String alertText = alert.getText();
  if (acceptNextAlert) {
    alert.accept();
  } else {
    alert.dismiss();
  }
  return alertText;
} finally {
  acceptNextAlert = true;
}
}
public class GetMulti {
    
    public  boolean textIsHtml = false;
 
    /**
     * Return the primary text content of the message.
     */
    public String getText(Part p) throws MessagingException, IOException {
        if (p.isMimeType("text/*")) {
            String s = (String)p.getContent();
            textIsHtml = p.isMimeType("text/html");
            return s;
        }
 
        if (p.isMimeType("multipart/alternative")) {
            // prefer html text over plain text
            Multipart mp = (Multipart)p.getContent();
            String text = null;
            for (int i = 0; i < mp.getCount(); i++) {
                Part bp = mp.getBodyPart(i);
                if (bp.isMimeType("text/plain")) {
                    if (text == null)
                        text = getText(bp);
                    continue;
                } else if (bp.isMimeType("text/html")) {
                    String s = getText(bp);
                    if (s != null)
                        return s;
                } else {
                    return getText(bp);
                }
            }
            return text;
        } else if (p.isMimeType("multipart/*")) {
            Multipart mp = (Multipart)p.getContent();
            for (int i = 0; i < mp.getCount(); i++) {
                String s = getText(mp.getBodyPart(i));
                if (s != null)
                    return s;
            }
        }
 
        return null;
    }
 
}
}